﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication

Public Class RSS
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim feeds As New SyndicationFeed

        feeds.Title = TextSyndicationContent.CreatePlaintextContent("Recent Updates to Phones")
        feeds.Description = TextSyndicationContent.CreatePlaintextContent("Recently Updated in the website")
        feeds.Links.Add(SyndicationLink.CreateSelfLink(New Uri(okURL("Default.aspx"))))
        feeds.Language = "en-uk"



        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("PrototypeConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT TOP 2 * from Phones ORDER BY ID DESC"
        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)
        Dim readDB = dbcomm.ExecuteReader()


        Dim myItems As New List(Of SyndicationItem)
        While readDB.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = mkRssTxt(readDB("PhoneName"))
            sItem.Summary = mkRssTxt(readDB("Review"))
            myItems.Add(sItem)
        End While

        feeds.Items = myItems

        Dim writeFeed As XmlWriter = XmlWriter.Create(Response.OutputStream)

        Dim formatFeed = New Rss20FeedFormatter(feeds)

        formatFeed.WriteTo(writeFeed)
        writeFeed.Close()
    End Sub


    Private Function mkRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)
    End Function
    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

End Class